//
//  U9LoginManager.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/8/8.
//  Copyright © 2017年 UUU9. All rights reserved.
//

#import "U9LoginManager.h"
#import "U9AccountModel.h"
#import "U9EncryptManager.h"
#import "U9IdentifyUserModel.h"

@implementation U9LoginManager

+ (void)loginWithPhoneNum:(NSString *)phoneNum andPassword:(NSString *)passwd andLoginType:(NSString *)type andCompletionHandle:(void(^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"loginname=%@&passwd=%@&logintype=%@",phoneNum,[U9EncryptManager convertPassword:passwd],type];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kLoginWithPhoneNum]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

+ (void)thrloginWithUserid:(NSString *)uid andthrToken:(NSString *)token andNickName:(NSString *)nickname andImage:(NSString *)image andType:(NSString *)type andCompletionHandle:(void(^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"ptuserid=%@&token=%@&utype=%@&nickname=%@&image=%@",uid,token,type,nickname,image];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kThirdLogin]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

+ (void)getPersonInfomationWithUserid:(NSString *)uid andSessionid:(NSString *)sessionid andCompletionHandle:(void (^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"userid=%@&sessionid=%@",uid,sessionid];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kGetPersonInfomation]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}


+ (void)logoutWithUserid:(NSString *)uid andSessionid:(NSString *)sessionid andCompletionHandle:(void (^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"userid=%@&sessionid=%@",uid,sessionid];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kLogout]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}


+ (void)getVerifyCodeWithPhone:(NSString *)phone andType:(NSString *)type andCompletionHandle:(void (^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"chktype=%@&phone=%@",type,phone];
    
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kGetVerifyCode]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

+ (void)registerAccountWithPhoneNum:(NSString *)phone andPWD:(NSString *)password andCode:(NSString *)code andLoginName:(NSString *)name andCompletionHandle:(void (^)(U9AccountModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"loginname=%@&passwd=%@&phone=%@&code=%@",name,[U9EncryptManager convertPassword:password],phone,code];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kRegisterAccountWithPhoneNum]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9AccountModel *model = [U9AccountModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}


+ (void)identifyUserWithUserid:(NSString *)uid andName:(NSString *)name andCode:(NSString *)code andCompletionHandle:(void (^)(U9IdentifyUserModel *account, NSError *error))complete{
    NSString *param = [NSString stringWithFormat:@"name=%@&idCard=%@&userid=%@",name,code,uid];
    [self EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kIdentifyUserMember]
           parameters:param
    completionHandler:^(id responseObj, NSError *error) {
        U9IdentifyUserModel *model = [U9IdentifyUserModel mj_objectWithKeyValues:responseObj];
        if (model.error_code != 0) {
            error = [self createError:model.error_msg];
        }
        complete(model,error);
    }];
}

/*
 #pragma mark 获取用户信息
 - (void)getPersonInfomationWithUserid:(NSString *)uid andSessionid:(NSString *)sessionid andBlock:(RequestFinishBlock)block{
 NSString *param = [NSString stringWithFormat:@"userid=%@&sessionid=%@",uid,sessionid];
 [self.manager POST:[NSString stringWithFormat:@"%@%@",Api_URL,kGetPersonInfomation]
 parameters:@{@"_secdata":_paramaters,@"param":[self base64Encoding:param]}
 progress:nil
 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
 NSData *resData = [[NSData alloc] initWithData:[[self base64Decoding:responseObject] dataUsingEncoding:NSUTF8StringEncoding]];
 id dict = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingAllowFragments error:nil];
 if (block) {
 block(dict);
 }
 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
 [userDefaults setBool:YES forKey:@"isOnline"];
 }
 failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
 [userDefaults setBool:false forKey:@"isOnline"];
 if (block) {
 block(@{@"error_code":@"1"});
 }
 }];
 }
 
 */
// [[DataHelper sharedDataHelper] getFaith:userid andSessionid:[[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"] andBlock:^(id result) {
@end
